#include "StdAfx.h"
#include "DDefaultLineCap.h"
#include "stdio.h"

#include "../ImagePro/ImageTIF.h"


#define CapLineTime_Default 1

DDefaultLineCap::DDefaultLineCap(void)
{
	m_nRotation     = 0;

	m_xCameraParam.fGain          = 0.25f;
	m_xCameraParam.fFrameSpeed    = CapLineTime_Default;
	m_xCameraParam.nPixelCombined = 1; 
	m_xCameraParam.fFrameSpeed = CapLineTime_Default;
}

DDefaultLineCap::~DDefaultLineCap(void)
{
}


int DDefaultLineCap::Initialize()
{
	PlugReg::m_xRegParam.dwReg = 1;
	sprintf(PlugReg::m_xRegParam.szMachineCode, "XE3PA11M2XSRBSVHKFCROLJP7GYE8UUMF3Y56SCR");
	sprintf(PlugReg::m_xRegParam.szRegisterCode,"4094MCB04OO60SO106OC00UOII4RCR390U990ZUM");


	char szModulePath[MAX_PATH] = "";
	char szTestFile[MAX_PATH] = "";

	GetModuleFileName(NULL,szModulePath,MAX_PATH);
	char* sTemp = strrchr(szModulePath,'\\'); 
	sTemp++;
	sTemp[0] = '\0'; 

	char sExtFile[2][5] = {"tif","tiff"};
	int nItem = -1;
	while( ++nItem < 2 )
	{
		sprintf(szTestFile,"%stest.%s",szModulePath,sExtFile[nItem] );
		if( ::GetFileAttributes(szTestFile) != 0xffffffff )
		{
			break;
		}
	}

	if( nItem >= 2 )
	{
		return -1;
	}

	m_xImgBitInfo.nImgDefine   = eImage_Define_Img16;

	int nBitsPerSample;

	CImageTIF xTif;
    xTif.GetImgBitPerSap(szTestFile,nBitsPerSample);
	if( nBitsPerSample < 9 || nBitsPerSample > 16)
	{
		return -1;
	}

	xTif.OpenImgFile(szTestFile,&m_xImgBitInfo);

	m_nReadLines = 0;

	m_nRotation = eCapRot_Mirror;

// 	m_xImagePro.Initialize();
// 	m_xImagePro.LoadFile( szTestFile );
// 
// 	zkImgBitInfo *pBitInfo = NULL;
// 	int nRet = m_xImagePro.GetImgBuffer(&pBitInfo);
// 	if( DD_ERR_SUCCESS != nRet )
// 	{
// 		return nRet;
// 	}
// 
// 	if( NULL != pBitInfo )
// 	{ 
// 		if( NULL != pBitInfo->pImgCapParam )
// 		{
// 			memcpy( pBitInfo->pImgCapParam,&m_xImgCapParma,sizeof(m_xImgCapParma) );
// 		}
// 		if( NULL != pBitInfo->pImgAssParam )
// 		{
// 			memset( pBitInfo->pImgAssParam,0,sizeof(zkImgAssParam) );
// 		}
// 	}


	return DD_ERR_SUCCESS;
}



int DDefaultLineCap::GetLineBitInfo(zkLineBitInfo *pLineBit)
{
	pLineBit->nImgDefine    = eImage_Define_Img16;
	pLineBit->nBitPerSample = 16;
	pLineBit->nSapPerPixel  = 1;
	pLineBit->fCapLineTime  = m_xCameraParam.fFrameSpeed;
	pLineBit->nWidth        = m_xImgBitInfo.nWidth;
	pLineBit->nLineSize     = pLineBit->nWidth * 2;

	return DD_ERR_SUCCESS;
}


int DDefaultLineCap::GetPlugDescription( zkCapDevDesc *pDescs,int &nCount )
{

	return DD_ERR_SUCCESS;
}


int DDefaultLineCap::SelectCap( zkCapDevDesc *pDesc )
{
	return DD_ERR_FAILURE;
}



#define DKQ_CHECK_ACTIVE  //if( NULL == m_pXtkDev ){return DD_ERR_NOTOPENDEV;}

int DDefaultLineCap::StartCapture()
{
	DKQ_CHECK_ACTIVE;
	return DD_ERR_SUCCESS;
}


int DDefaultLineCap::StopCapture()
{
	DKQ_CHECK_ACTIVE;
	return DD_ERR_SUCCESS;
}

int DDefaultLineCap::GetLinePixelCount(int &nPixelCount)
{
	DKQ_CHECK_ACTIVE;
	nPixelCount = m_xImgBitInfo.nWidth;

	return DD_ERR_SUCCESS;
}

int DDefaultLineCap::GetLinePixel(void *pData)
{
	DKQ_CHECK_ACTIVE;

	Sleep( (int)m_xCameraParam.fFrameSpeed );

	if( m_nRotation & eCapRot_Mirror )
	{
		int   nWidth   = m_xImgBitInfo.nWidth;
		WORD *pSrc = (WORD*)m_xImgBitInfo.pImageData + m_nReadLines * nWidth;
        WORD *pDst = (WORD*)pData;
		for(int i=0;i<nWidth;i++)
		{
			pDst[i] = pSrc[nWidth-i-1];
		}
	}
	else
	{
		int   nSize    = m_xImgBitInfo.nWidth*sizeof(WORD);
		int   nReadPos = m_nReadLines * m_xImgBitInfo.nWidth;
		WORD *p = (WORD*)m_xImgBitInfo.pImageData;
		memcpy( pData,p+nReadPos,nSize );
	}

	m_nReadLines++;

	if( m_nReadLines >= m_xImgBitInfo.nHeight )
	{
		m_nReadLines = 0;
	}
	
	return DD_ERR_SUCCESS;
}


int DDefaultLineCap::OpCaptureParam(void *pVoid)
{
	if( NULL == pVoid )
	{
		return DD_ERR_PARAMETER;
	}
	int nRet = DD_ERR_SUCCESS;

	zkFCaptureParam *pFCapParam = (zkFCaptureParam*)pVoid;

	switch( pFCapParam->dwType )
	{
	case eCap_Parma_ImageViewRect:
		{
			RECT *pTemp = (RECT *)pFCapParam->pParam; 
		}
		break;
	case eCap_Parma_GetRegisterInfo:
		{
			pFCapParam->bAutoDel = FALSE;
			pFCapParam->pParam   = (void*)&(PlugReg::m_xRegParam);
		}
		break;
	case eCap_Parma_SetRegisterInfo:
		{
		}
		break;
	case eCap_Parma_GetCamera_Param:
		{
 			pFCapParam->bAutoDel   = FALSE;
 			pFCapParam->nParamSize = sizeof(zkCameraParam);
 			pFCapParam->pParam     = &m_xCameraParam;	   
		}
		break;
	case eCap_Parma_SetCamera_Param: 
		{
			zkCameraParam *pParam = (zkCameraParam*)(pFCapParam->pParam);
			m_xCameraParam = *pParam;
		}
		break;
	case eCap_Parma_GetCameraList_Gain:
		{
           float *fPlus = (float*)m_szTemp;

		   fPlus[0] = 0.1f;
		   fPlus[1] = 0.25f;
		   fPlus[2] = 0.5f;
		   fPlus[3] = 1;
		   fPlus[4] = 2;
		   fPlus[5] = 5;
		   fPlus[6] = 10;
		   fPlus[7] = 20;

		   pFCapParam->bAutoDel   = FALSE;
		   pFCapParam->nParamSize = 8 * sizeof(float);
		   pFCapParam->pParam     = (void*)m_szTemp;
		}
		break;
	case eCap_Parma_GetCameraList_FrameSpeed:  
		{
			float *fFrameSpeed = (float*)m_szTemp;

			fFrameSpeed[0] = 1;
			fFrameSpeed[1] = 2;
			fFrameSpeed[2] = 4;
			fFrameSpeed[3] = 8;
			fFrameSpeed[4] = 20;
			fFrameSpeed[5] = 40;

			pFCapParam->bAutoDel   = FALSE;
			pFCapParam->nParamSize = 6 * sizeof(float);
			pFCapParam->pParam     = (void*)m_szTemp;
		}
		break;
	case eCap_Parma_GetCameraList_PixelCombined:
		{
			int *nPixelCombined = (int*)m_szTemp;

			nPixelCombined[0] = 1;
			nPixelCombined[1] = 2;
			nPixelCombined[2] = 4;

			pFCapParam->bAutoDel   = FALSE;
			pFCapParam->nParamSize = 3 * sizeof(int);
			pFCapParam->pParam     = (void*)m_szTemp;
		}
		break;
	case eCap_Parma_Pixel21:
		{
			
		}
		break;
	case eCap_Parma_GetCapNetInfo:
		{
			
		}
		break;
	case eCap_Parma_SetCapNetInfo:
		{

		}
		break;
	default:
		nRet = DD_ERR_FAILURE;
		break;
	}
	
	return nRet;
}



